﻿<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(!isset($_SESSION['u_nic'])) {
	if(isset($_POST['submit'])) {
		if(	$_POST['nom'] != "" AND 
			$_POST['prenom'] != "" AND
			$_POST['adresse'] != "" AND
			$_POST['cp'] != "" AND
			$_POST['ville'] != "" AND
			$_POST['pays'] != "" AND
			$_POST['mail'] != "" AND
			$_POST['civilite'] != "" AND
			$_POST['captcha'] != "" AND
			$_POST['cgv'] == 1 AND
			$_POST['in'] == 1) {
				if($_POST['captcha'] == $_SESSION['captchaResult']) {
					$civilite = preg_replace('#([^a-zA-Z])+#i', '', $_POST['civilite']);
					$nom = preg_replace('#([^a-zA-Z \-éèêàâôî])+#i', '', strtoupper($_POST['nom']));
					$prenom = preg_replace('#([^a-zA-Z \-éèêàâôî])+#i', '', ucfirst($_POST['prenom']));
					$adresse = preg_replace('#([^a-zA-Z0-9 \-éèêàâôî])+#i', '', $_POST['adresse']);
					$cp = intval($_POST['cp']);
					$ville = preg_replace('#([^a-zA-Z \-éèêàâôî])+#i', '', ucfirst($_POST['ville']));
					$pays = intval($_POST['pays']);
					$mail = preg_replace('#([^a-zA-Z0-9@.\-\_])+#i', '', strtolower($_POST['mail']));		
					$naissance = mktime(0,0,0,intval($_POST['naissance_mon']),intval($_POST['naissance_day']),intval($_POST['naissance_yea']));
					$tel = preg_replace('#([^0-9\+\.])+#i', '', $_POST['tel']);
					if(preg_match('`^\+[0-9]{1,3}\.[0-9]{4,12}$`',$tel)) {
						if(!preg_match('`^\+33\.6[0-9]{8}$`',$tel)) {
							$ip = $_SERVER['REMOTE_ADDR'];
							$chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
								$nb_caract = 8;
								$pass = "";
								for($u = 1; $u <= $nb_caract; $u++) {
									$nb = strlen($chaine);
									$nb = mt_rand(0,($nb-1));
									$pass.=$chaine[$nb];
								}
							$dejacompte = mysql_fetch_array(query("SELECT COUNT(*) AS compte FROM users WHERE u_email = '".$mail."'"));
							if($dejacompte['compte'] == 0) {
								if(date('Y', $naissance) > date('Y', time() -  3600*24*365*25)) {
									query("INSERT INTO users(u_email, u_register_ip, u_nom, u_prenom, u_adresse, u_cp, u_ville, u_pays, u_naissance, u_tel, u_civilite)
															VALUES('$mail', '$ip', '$nom', '$prenom', '$adresse', '$cp', '$ville', '$pays', '".date('Y-m-d',$naissance)." 00:00:00', '$tel', '$civilite')");
									$nic = mysql_fetch_array(query("SELECT u_nic FROM users WHERE u_email = '".$mail."'"));
									query("INSERT INTO users_password(u_nic, u_password, u_crypt) VALUES('".$nic['u_nic']."', '".sha1($pass)."', 1)");
									$identif = array();
									$identif['{title}'] = '[Breizh Heberg] Bienvenue sur Breizh Heberg';
									$identif['{contenu}'] = '
									Bonjour et bienvenue sur le service de Breizh Héberg,<br />
									Voici vos informations de conexion à Breizh Héberg:<br /> 
									<strong>Login</strong> : '.$nic['u_nic'].'-BW<br />
									<strong>Mot de Passe</strong> : '.$pass.'<br /><br />
									Nous vous remercions de conserver celui-ci précieusement et nous vous souhaitons une agréable utilisation de nos services.';
									$message = str_replace(array_keys($identif), array_values($identif), file_get_contents("tpl/mail-default.html"));
									$headers = "MIME-Version: 1.0\n";
									$headers .= "Content-type: text/html; charset=utf-8\n";
									$headers .= "From: Breizh Héberg Bot <robot@breizh-heberg.net>\n";
									$headers .= "Reply-to: Support de Breizh Héberg <support@breizh-heberg.net>\n";
									mail($mail, $identif['{title}'], $message, $headers);
									$error = 4;
								}
								else {
									$error = 5;
								}
							}
							else {
								$error = 6;
							}
						}
						else {
							$error = 8;
						}
					}
					else {
						$error = 7;
					}
				}
				else {
					$error = 3;				
				}
		}
		else {
			$error = 2;
		}
		echo $error;
		$title = "Inscription";
		$tplinc = "site-inscription2";
		$parse = 1;
		$tpl->assign(array(
			'error' => $error
		));
	
	}
	else {
		$title = "Inscription";
		$tplinc = "site-inscription";
		$parse = 1;
		include ROOT.'php/lib/registrars/online.inc.php';
		$_domgest = new bh_doms_gest();
		$_liste_pays = $_domgest->codes_pays();
		$_domgest->deconnect();
		$tpl->assign(array(
			'code_pays' => $_liste_pays['retfields']
		));
	}
}
?>